package com.ddxz.middleground.master.controller;

import com.ddxz.middleground.master.dto.MasterHeader;
import com.ddxz.middleground.master.dto.menu.MenuInfo;
import com.ddxz.middleground.master.service.MenuService;
import com.ddxz.tool.web.annotation.DdxzRequestHeader;
import com.ddxz.tool.web.dto.DdxzResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/7/30
 */

@RestController
@Api(tags = "菜单管理")
public class MenuController {
    @ApiOperation(value = "检索菜单", consumes = "application/json", produces = "application/json",
            notes = "")
    @RequestMapping(value = "/menu/{appId}/tree", method = RequestMethod.GET)
    public DdxzResponse menuTree(@DdxzRequestHeader MasterHeader header,
                                 @PathVariable("appId") String appId,
                                 @RequestParam(name = "keyword", required = false, defaultValue = "") String keyword,
                                 @RequestParam(name = "permissionId", required = false, defaultValue = "") String permissionId) {
        return service.getAppMenuTree(header, appId, keyword, permissionId);
    }

    @ApiOperation(value = "保存菜单", consumes = "application/json", produces = "application/json",
            notes = "")
    @RequestMapping(value = "/menu/save", method = RequestMethod.POST)
    public DdxzResponse menuSave(@DdxzRequestHeader MasterHeader header, @RequestBody MenuInfo menuInfo) {
        return service.menuSave(header, menuInfo);
    }

    @ApiOperation(value = "查看菜单", consumes = "application/json", produces = "application/json",
            notes = "")
    @RequestMapping(value = "/menu/{menuId}/detail", method = RequestMethod.GET)
    public DdxzResponse menuDetail(@DdxzRequestHeader MasterHeader header, @PathVariable("menuId") String menuId) {
        return service.menuDetail(header, menuId);
    }

    @ApiOperation(value = "删除菜单", consumes = "application/json", produces = "application/json",
            notes = "")
    @RequestMapping(value = "/menu/{menuId}/remove", method = RequestMethod.DELETE)
    public DdxzResponse menuRemove(@DdxzRequestHeader MasterHeader header, @PathVariable("menuId") String menuId) {
        return service.menuRemove(header, menuId);
    }

    @ApiOperation(value = "移动菜单", consumes = "application/json", produces = "application/json",
            notes = "对菜单进行移动操作")
    @RequestMapping(value = "/menu/{menuId}/move", method = RequestMethod.PUT)
    public DdxzResponse menuMove(@DdxzRequestHeader MasterHeader header, @RequestParam(name = "direction") Integer direction, @PathVariable("menuId") String menuId) {
        return service.menuMove(header, direction, menuId);
    }

    @ApiOperation(value = "加载菜单", consumes = "application/json", produces = "application/json",
            notes = "根据用户信息加载当前用户在指定应用下可以使用的菜单树")
    @RequestMapping(value = "/menu/valid/tree", method = RequestMethod.GET)
    public DdxzResponse getMenuTree(@DdxzRequestHeader MasterHeader header, @RequestParam(name = "appId") String appId) {
        return service.getAppMenuTree(header, appId);
    }

    public MenuController(MenuService service) {
        this.service = service;
    }

    private final MenuService service;
}
